package com.m4f.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import com.m4f.utils.StackTraceUtil;
import java.util.Collections;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

public abstract class BaseController {
	
	@Autowired
	private ReloadableResourceBundleMessageSource messageSource;
	protected Cache cache;
	private static final Logger LOGGER = Logger.getLogger(BaseController.class.getName());
	
	protected final static String PRESELECTED_COMPANIES_KEY = "preselectedCompanies"; 
	
	public BaseController() throws CacheException {
		this.cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());

	}
	
	public String getMessage(String msg, Object... args) {
		List<Object> variableParts = new ArrayList<Object>();
		if(args != null) {
			for(Object arg : args) {
				variableParts.add(arg);
			}
		}
		String message = "????" + msg + "????";
		try {
			message = this.messageSource.getMessage(msg,
					variableParts.toArray(), null);
		} catch(Exception e) {
			LOGGER.info(StackTraceUtil.getStackTrace(e));
		}
		return message;
	}
	
	public boolean isSupportedMimeType(List<String> supportedMimeTypes, 
			String targetMimeType) {
		for(String mimeType : supportedMimeTypes) {
			if(mimeType.equals(targetMimeType)) {
				return true;
			}
		}
		return false;
	}
	
}